package com.bkq.cb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.MailTo;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Login extends Activity {
	private Button exitBnt,registerBnt,loginBnt;
	private SqliteServices service = null;
	private EditText username, password;
	private Cursor mCursor;
	int REQUEST_CODE = 10;
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
     setContentView(R.layout.login);
   
     exitBnt = (Button)findViewById(R.id.backIma_login);
     registerBnt = (Button)findViewById(R.id.registerIma_login);
     loginBnt = (Button)findViewById(R.id.loginBnt_login);
     username = (EditText)findViewById(R.id.usernameTxt_login);
     password = (EditText)findViewById(R.id.passwordTxt_login);
     exitBnt.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
	});
     registerBnt.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			startActivity(new Intent(Login.this,Register.class));
		}
	});
     service = new SqliteServices(this);
     service.openDatabase();
     loginBnt.setOnClickListener(new View.OnClickListener() {
	String idUser,nameUser;	
		@Override
		public void onClick(View v) {
			boolean bool = false;
			// TODO Auto-generated method stub
			mCursor = service.queryData("select * from tblUser");

			if(mCursor.moveToFirst())
	        {     		
			    if((mCursor.getString(3).equals(password.getText().toString())) && 
			        (mCursor.getString(2).equals(username.getText().toString())))
			    {
			    	  idUser = mCursor.getString(0).toString();
			    	  nameUser = mCursor.getString(1).toString();
			    			bool = true;
			        		username.setText("");
			        		password.setText("");
			        		AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
			        		alertDialog.setTitle("Login succesfuly!");
			        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			        		   public void onClick(DialogInterface dialog, int which) {
			        		      
			        			   Intent i = new Intent(Login.this, MainScreen.class);
			        			   i.putExtra("nameUser", nameUser);
			        			   i.putExtra("idUser", idUser);
			        			   startActivity(i);
			        		   }
			        		});		  
			        		alertDialog.setIcon(R.drawable.success);
			        		alertDialog.show();
			        		
			        	
        		}
	        	while(mCursor.moveToNext())
	        	{
	        				if((mCursor.getString(3).equals(password.getText().toString())) && 
				        			(mCursor.getString(2).equals(username.getText().toString())))
				        	{
	        					 idUser = mCursor.getString(0).toString();
	        					 nameUser = mCursor.getString(1).toString();
	        					bool = true;
				        		username.setText("");
				        		password.setText("");
				        		AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
				        		alertDialog.setTitle("Login succesfuly!");
				        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				        		   public void onClick(DialogInterface dialog, int which) {
				        		      
				        			   Intent i = new Intent(Login.this, MainScreen.class);
				        			   i.putExtra("nameUser", nameUser);
				        			   i.putExtra("idUser", idUser);
				        			   startActivity(i);


				        		   }
				        		});		  
				        		alertDialog.setIcon(R.drawable.success);
				        		alertDialog.show();
				       		}
	        	}
	        	mCursor.close();
	        }
			if(bool == false) 
			{
				{
	        		username.setText("");
	        		password.setText("");
	        		Toast.makeText(getBaseContext(), "Username doesn't exist", Toast.LENGTH_SHORT).show();
	        	}
			}
		}
		
	});
   
}
	
}
